package com.markerhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysDictDetail;
import com.markerhub.service.SysDictDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 数据字典详情Controller
 */
@Api(tags = "字典详情管理")
@RestController
@RequestMapping("/api/sys-dict-detail")
public class SysDictDetailController extends BaseController {

    @Autowired
    private SysDictDetailService sysDictDetailService;

    /**
     * 获取数据字典详情列表
     */
    @PreAuthorize("hasAuthority('sys:dict-detail:list')")
    @ApiOperation(value = "获取字典详情列表", notes = "获取所有数据字典详情信息，支持分页查询")
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) Long dictId, 
                      @RequestParam(required = false) String label,
                      @RequestParam(required = false) String value,
                      @RequestParam(defaultValue = "1") Integer pageNum,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        // 使用前端传递的pageNum和pageSize参数创建分页对象
        Page<SysDictDetail> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
        if (dictId != null) {
            wrapper.eq("dict_id", dictId);
        }
        if (label != null && !label.isEmpty()) {
            wrapper.like("label", label);
        }
        if (value != null && !value.isEmpty()) {
            wrapper.like("value", value);
        }
        // 按创建时间倒序排序
        wrapper.orderByDesc("created");
        // 执行分页查询
        Page<SysDictDetail> pageData = sysDictDetailService.page(page, wrapper);
        return Result.succ(200, "获取数据字典详情列表成功", pageData);
    }
    
    /**
     * 查询字典详情 - 分页
     */
    @PreAuthorize("hasAuthority('sys:dict-detail:list')")
    @ApiOperation(value = "查询字典详情", notes = "根据条件查询字典详情")
    @GetMapping("/query")
    public Result query() {
        // 使用BaseController中的getPage()方法获取分页参数
        Page<SysDictDetail> page = getPage();
        // 执行分页查询
        Page<SysDictDetail> pageData = sysDictDetailService.page(page);
        return Result.succ(200, "查询字典详情成功", pageData);
    }

    /**
     * 获取数据字典详情
     */
    @PreAuthorize("hasAuthority('sys:dict-detail:list')")
    @ApiOperation(value = "获取数据字典详情", notes = "根据ID获取数据字典详情详细信息")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        SysDictDetail dictDetail = sysDictDetailService.getById(id);
        return Result.succ(200, "获取数据字典详情信息成功", dictDetail);
    }
    
    /**
     * 根据字典类型获取字典详情列表
     */
    @ApiOperation(value = "根据字典类型获取字典详情列表", notes = "根据字典类型编码获取对应的字典详情列表")
    @GetMapping("/by-type/{typeCode}")
    public Result byType(@PathVariable String typeCode) {
        QueryWrapper<SysDictDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_code", typeCode);
        List<SysDictDetail> dictDetails = sysDictDetailService.list(queryWrapper);
        return Result.succ(200, "获取字典详情列表成功", dictDetails);
    }

    /**
     * 创建数据字典详情
     */
    @PreAuthorize("hasAuthority('sys:dict-detail:add')")
    @ApiOperation(value = "创建数据字典详情", notes = "新增数据字典详情信息")
    @PostMapping("/add")
    public Result add(@RequestBody SysDictDetail dictDetail) {
        // 设置创建时间
        dictDetail.setCreated(new Date());
        // 设置更新时间
        dictDetail.setUpdated(new Date());
        boolean saved = sysDictDetailService.save(dictDetail);
        return saved ? Result.succ(200, "创建数据字典详情成功", null) : Result.fail("创建数据字典详情失败");
    }

    /**
     * 更新数据字典详情
     */
    @PreAuthorize("hasAuthority('sys:dict-detail:update')")
    @ApiOperation(value = "更新数据字典详情", notes = "更新数据字典详情信息")
    @PutMapping("/update")
    public Result update(@RequestBody SysDictDetail dictDetail) {
        // 设置更新时间
        dictDetail.setUpdated(new Date());
        boolean updated = sysDictDetailService.updateById(dictDetail);
        return updated ? Result.succ(200, "更新数据字典详情成功", null) : Result.fail("更新数据字典详情失败");
    }

    /**
     * 删除数据字典详情（支持单删和批量删除）
     */
    @PreAuthorize("hasAuthority('sys:dict-detail:delete')")
    @ApiOperation(value = "删除数据字典详情", notes = "根据ID删除单个数据字典详情")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean deleted = sysDictDetailService.removeById(id);
        return deleted ? Result.succ(200, "删除数据字典详情成功", null) : Result.fail("删除数据字典详情失败");
    }
    

}